﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_SYS_FILE_POWER
// * CreateDate    : 2013-12-04 23:19:02
// * Creater       : CodeSmith
// * LastChangeDate: 2013-12-04 23:19:02
// * LastChanger   : 2013-12-04 23:19:02
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Sys
{
    /// <summary>
    /// SysFilePower
    /// </summary>
    public partial class SysFilePowerBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objSysFilePowerEntity">Entity</param>
        /// <returns>Entity</returns>
        public static SysFilePowerEntity GetSysFilePower(SysFilePowerEntity objSysFilePowerEntity)
        {
            return DataAccess.SelectSingle<SysFilePowerEntity>(objSysFilePowerEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objSysFilePowerEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertSysFilePower(SysFilePowerEntity objSysFilePowerEntity)
        {
            return DataAccess.Insert(objSysFilePowerEntity);
        }

        public static bool InsertSysFilePower(List<SysFilePowerEntity> objSysFilePowerEntityList)
        {
            return DataAccess.Insert(objSysFilePowerEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objSysFilePowerEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateSysFilePower(SysFilePowerEntity objSysFilePowerEntity)
        {
            return DataAccess.Update(objSysFilePowerEntity);
        }

        public static bool UpdateSysFilePower(List<SysFilePowerEntity> objSysFilePowerEntityList)
        {
            return DataAccess.Update<SysFilePowerEntity>(objSysFilePowerEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objSysFilePowerEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteSysFilePower(SysFilePowerEntity objSysFilePowerEntity)
        {
            return DataAccess.Delete(objSysFilePowerEntity);
        }

        public static bool DeleteSysFilePower(List<SysFilePowerEntity> objSysFilePowerEntityList)
        {
            return DataAccess.Delete<SysFilePowerEntity>(objSysFilePowerEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetSysFilePowerAll()
        {
            return DataAccess.SelectAllDataSet<SysFilePowerEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<SysFilePowerEntity> GetSysFilePowerAllList()
        {
            return DataAccess.SelectAll<SysFilePowerEntity>();
        }
        #endregion

        public static List<SysFilePowerEntity> GetAllListByFileId(string fileId)
        {
            return DataAccess.Select<SysFilePowerEntity>(new SysFilePowerEntity { FileId = fileId });
        }

        public static List<SysFilePowerEntity> GetSysFileDutyPowerAllList(string fileId)
        {
            return DataAccess.Select<SysFilePowerEntity>("select * from SYS_FILE_POWER where FILE_ID='" + fileId + "' AND USER_NO IS NULL");
        }

        public static List<SysFilePowerEntity> GetSysFileUserPowerAllList(string fileId)
        {
            return DataAccess.Select<SysFilePowerEntity>("select * from SYS_FILE_POWER where FILE_ID='" + fileId + "' AND DUTY_NO IS NULL");
        }

        public static DataSet GetAllByCondition(int pageIndex, int pageCount, string condition)
        {
            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from SYS_FILE) as cnt
from SYS_FILE
where id not in (
    select top ({1}*({2}-1)) id from SYS_FILE order by created_date
) order by created_date desc";
                sqlSource = string.Format(sqlSource, pageCount, pageCount, pageIndex);
                sql = @"select sf.*,pu1.firstName as CREATED_USER,pu2.firstName as UPDATED_USER
from ({0}) sf
left join BPMDB.dbo.PW_User pu1 on sf.CREATED_BY=pu1.userNo
left join BPMDB.dbo.PW_User pu2 on sf.CREATED_BY=pu2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date
) order by created_date desc";
                string sqlSource = @"select sf.*,pu1.firstName as CREATED_USER,pu2.firstName as UPDATED_USER 
from SYS_FILE sf
left join BPMDB.dbo.PW_User pu1 on sf.CREATED_BY=pu1.userNo
left join BPMDB.dbo.PW_User pu2 on sf.CREATED_BY=pu2.userNo
where 1=1 ";
                sqlSource += condition;
                sql = string.Format(sql, pageCount, pageCount, pageIndex, sqlSource);
            }
            return DataAccess.SelectDataSet(sql);
        }

        public static DataSet GetAllByPowerCondition(int pageIndex, int pageCount,string userNo,string[] dutyNos, string condition)
        {
            string sql = "";
            if (string.IsNullOrEmpty(condition))
            {
                string sqlSource = @"select top {0} *,(select count(1) from SYS_FILE) as cnt
from SYS_FILE
where id not in (
    select top ({1}*({2}-1)) sf.id from SYS_FILE sf left join SYS_FILE_POWER sfp on sf.ID=sfp.FILE_ID
    where sfp.USER_NO='{3}' or sfp.DUTY_NO in ({4})
    order by sf.created_date
) order by created_date desc";
                string duty = "";
                for(int i=0;i<dutyNos.Length;i++)
                {
                    if (i != dutyNos.Length - 1)
                    {
                        duty += "'" + dutyNos[i]+"',";
                    }
                    else
                    {
                        duty += "'" + dutyNos[i] + "'";
                    }
                }
                sqlSource = string.Format(sqlSource, pageCount, pageCount, pageIndex, userNo, duty);
                sql = @"select sf.*,pu1.firstName as CREATED_USER,pu2.firstName as UPDATED_USER
from ({0}) sf
left join BPMDB.dbo.PW_User pu1 on sf.CREATED_BY=pu1.userNo
left join BPMDB.dbo.PW_User pu2 on sf.CREATED_BY=pu2.userNo";
                sql = string.Format(sql, sqlSource);
            }
            else
            {
                sql = @"select top {0} *,(select count(1) from ({3}) t) as cnt
from ({3}) t
where t.id not in (
    select top ({1}*({2}-1)) id from ({3}) t order by created_date
) order by created_date desc";
                string sqlSource = @"select sf.*,pu1.firstName as CREATED_USER,pu2.firstName as UPDATED_USER 
from SYS_FILE sf
left join BPMDB.dbo.PW_User pu1 on sf.CREATED_BY=pu1.userNo
left join BPMDB.dbo.PW_User pu2 on sf.CREATED_BY=pu2.userNo
left join SYS_FILE_POWER sfp on sf.ID=sfp.FILE_ID
where 1=1 and sfp.USER_NO='{0}' or sfp.DUTY_NO in ({1})";
                string duty = "";
                for (int i = 0; i < dutyNos.Length; i++)
                {
                    if (i != dutyNos.Length - 1)
                    {
                        duty += "'" + dutyNos[i] + "',";
                    }
                    else
                    {
                        duty += "'" + dutyNos[i] + "'";
                    }
                }
                sqlSource = string.Format(sqlSource, userNo, duty);

                sqlSource += condition;
                sql = string.Format(sql, pageCount, pageCount, pageIndex, sqlSource);
            }
            return DataAccess.SelectDataSet(sql);
        }

    }


}

